package com.token.utils;

import com.google.common.collect.Lists;
import com.token.model.dto.Person;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @auther: henriy
 * @Date: 2024/7/8
 */
public class PersonMapTest {

    public static void main(String[] args) {
        List<Person> personList = Lists.newArrayList();

        for (int i = 1; i < 5; i++) {
            Person person = new Person();
            person.setSex("男");
            person.setAge(i);
            person.setName("" + i + 1);
            personList.add(person);
        }

        Map<Integer, Person> singleMap = personList.stream()
                .collect(Collectors.toMap(Person::getAge, Function.identity()));
        System.out.println("singleMap---" + singleMap);

        Map<String, Person> doubleMap = personList.stream()
                .collect(Collectors.toMap(Person::getSex, Function.identity(), (t1, t2) -> t2));
        System.out.println("doubleMap---" + doubleMap);

        Map<Integer, String> valueMap = personList.stream()
                .collect(Collectors.toMap(Person::getAge, Person::getName));
        System.out.println("valueMap---" + valueMap);
    }

}
